﻿@{
    Html.EnableClientValidation();
    Html.EnableUnobtrusiveJavaScript();

    var grid = Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "gvEditing";
            settings.KeyFieldName = "IdReceta";
            settings.CallbackRouteValues = new { Controller = "Receta", Action = "EditRecetaPartial" };
            settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Receta", Action = "EditRecetaAddNewPartial" };
            settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Receta", Action = "EditRecetaUpdatePartial" };
            settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Receta", Action = "EditRecetaDeletePartial" };
            settings.SettingsEditing.Mode = GridViewEditingMode.EditFormAndDisplayRow;
            
            
            settings.SettingsBehavior.ConfirmDelete = true;
            settings.SettingsPopup.EditForm.Width = 600;
            settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.Center;
            settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
            settings.SettingsPager.FirstPageButton.Visible = true;
            settings.SettingsPager.LastPageButton.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };
            
            
            settings.Settings.ShowFilterRow = true;
            settings.Settings.ShowFilterRowMenu = true;
            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowClearFilterButton = true;
            
            settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowNewButtonInHeader = ViewBag.PermisoCreate;
            settings.CommandColumn.ShowEditButton = ViewBag.PermisoEdit;
            settings.CommandColumn.ShowDeleteButton = ViewBag.PermisoDelete;

            settings.Columns.Add("IdReceta");
            settings.Columns.Add("Nombre");
            settings.Columns.Add("Descripcion");

            settings.Columns.Add(column =>
            {
                column.FieldName = "FechaAlta";
                column.Caption = "FechaAlta";
                column.ColumnType = MVCxGridViewColumnType.DateEdit;
                column.PropertiesEdit.DisplayFormatString = "dd/MM/yyyy";
                column.Width = 100;
            });

            settings.Columns.Add(columnCheck =>
            {
                columnCheck.FieldName = "Predeterminada";
                columnCheck.ColumnType = MVCxGridViewColumnType.CheckBox;

                var checkBoxProperties = columnCheck.PropertiesEdit as CheckBoxProperties;
                checkBoxProperties.ValueUnchecked = null;
                columnCheck.Width = 50;
            });

            settings.Columns.Add(
                column =>
                {
                    column.FieldName = "Producto.IdProducto";
                    column.Caption = "Producto";

                    column.ColumnType = MVCxGridViewColumnType.ComboBox;
                    var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                    comboBoxProperties.DataSource = ViewBag.Producto;
                    comboBoxProperties.TextField = "Nombre";
                    comboBoxProperties.ValueField = "IdProducto";
                    comboBoxProperties.ValueType = typeof(int);
                    comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
                });
                   

            settings.SettingsBehavior.AllowFocusedRow = true;

            //settings.SettingsDetail.AllowOnlyOneMasterRowExpanded = true;
            //settings.SettingsDetail.ShowDetailRow = true;

             settings.SetEditFormTemplateContent(c => {

                ViewContext.Writer.Write("<div class=\"edit_form\">");
                 
                 var receta = ViewData["RecetaEdit"] != null ? ViewData["RecetaEdit"] : c.DataItem;
                 
                            ViewContext.Writer.Write("<div class=\"line\">");
							Html.DevExpress().Label(
								settingsCliente => 
                                {
                                    settingsCliente.ControlStyle.CssClass = "label";
                                    settingsCliente.AssociatedControlName = "Producto.IdProducto";
                                    settingsCliente.Text = "Producto:";
								}
							).GetHtml();
							Html.DevExpress().ComboBox(
                                settingsCliente =>
                                {
                                    settingsCliente.Name = "Producto.IdProducto";
                                    settingsCliente.ControlStyle.CssClass = "editor";
                                    settingsCliente.Properties.TextField = "Nombre";
                                    settingsCliente.Properties.ValueField = "IdProducto";
                                    settingsCliente.Properties.ValueType = typeof(int);
                                    settingsCliente.ShowModelErrors = true;
                                    settingsCliente.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
								}
                            ).BindList(ViewBag.Producto).Bind(DataBinder.Eval(receta, "Producto.IdProducto")).GetHtml();
							ViewContext.Writer.Write("</div>");

                            ViewContext.Writer.Write("<div class=\"line\">");
                            Html.DevExpress().Label(
                                settingsDate =>
                                {
                                    settingsDate.ControlStyle.CssClass = "label";
                                    settingsDate.AssociatedControlName = "Nombre";
                                    settingsDate.Text = "Nombre:";                                    
                                }
                            ).GetHtml();

                            Html.DevExpress().TextBox(
                                settingsDate =>
                                {
                                    settingsDate.Name = "Nombre";                                    
                                    settingsDate.ShowModelErrors = true;
                                    settingsDate.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                                }
                            ).Bind(DataBinder.Eval(receta, "Nombre")).GetHtml();
                            ViewContext.Writer.Write("</div>");


                            ViewContext.Writer.Write("<div class=\"line\">");
                            Html.DevExpress().Label(
                                settingsDate =>
                                {
                                    settingsDate.ControlStyle.CssClass = "label";
                                    settingsDate.AssociatedControlName = "Descripcion";
                                    settingsDate.Text = "Descripcion:";
                                }
                            ).GetHtml();

                            Html.DevExpress().TextBox(
                                settingsDate =>
                                {
                                    settingsDate.Name = "Descripcion";
                                    settingsDate.ShowModelErrors = true;
                                    settingsDate.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                                }
                            ).Bind(DataBinder.Eval(receta, "Descripcion")).GetHtml();
                            ViewContext.Writer.Write("</div>");


                            ViewContext.Writer.Write("<div class=\"line\">");
                            Html.DevExpress().Label(
                                settingsDate =>
                                {
                                    settingsDate.ControlStyle.CssClass = "label";
                                    settingsDate.AssociatedControlName = "Predeterminada";
                                    settingsDate.Text = "Predeterminada:";
                                }
                            ).GetHtml();

                            Html.DevExpress().CheckBox(
                                settingsPred =>
                                {
                                    settingsPred.Name = "Predeterminada";
                                    settingsPred.ShowModelErrors = true;
                                    settingsPred.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                                }
                            ).Bind(DataBinder.Eval(receta, "Predeterminada")).GetHtml();
                            ViewContext.Writer.Write("</div>");
                 

                            ViewContext.Writer.Write("<div class=\"line\">" + "<br />");
                            var idreceta = DataBinder.Eval(c.DataItem, "IdReceta");
                            if (idreceta == null)
                                idreceta = 0;
                            Html.RenderAction("EditRecetaDetallePartial", new { IdReceta = idreceta });
                            ViewContext.Writer.Write("</div>");

                            ViewContext.Writer.Write("<div class=\"line\">");
                            Html.DevExpress().Label(
                                edtSettings =>
                                {
                                    edtSettings.ControlStyle.CssClass = "label";
                                }
                            )
                            .Render();
                            Html.DevExpress().Button(
                                btnSettings =>
                                {
                                    btnSettings.Name = "btnUpdate";
                                    btnSettings.ControlStyle.CssClass = "button";
                                    btnSettings.Text = "Guardar";
                                    btnSettings.ClientSideEvents.Click = "function(s, e){ gvEditing.UpdateEdit(); }";
                                }
                            )
                            .Render();
                            Html.DevExpress().Button(
                                btnSettings =>
                                {
                                    btnSettings.Name = "btnCancel";
                                    btnSettings.ControlStyle.CssClass = "button";
                                    btnSettings.Text = "Cancelar";
                                    btnSettings.ClientSideEvents.Click = "function(s, e){ document.location='" + DevExpressHelper.GetUrl(new { Controller = "Receta", Action = "EditRecetaCancelPartial", IdReceta = idreceta }) + "'; }";
                                }
                            )
                            .Render();
                            ViewContext.Writer.Write("</div>");   
                 
                 
                         ViewContext.Writer.Write("</div>");
                        });
            
            settings.SetDetailRowTemplateContent(c =>
            {
                Html.RenderAction("EditRecetaDetallePartial", new { IdReceta = DataBinder.Eval(c.DataItem, "IdReceta") });
            });

            settings.BeforeGetCallbackResult = (sender, e) =>
            {
                MVCxGridView grid2 = (MVCxGridView)sender;
                if (ViewData["Receta"] != null)
                {
                    grid2.MakeRowVisible(ViewData["Receta"]);
                    grid2.DetailRows.ExpandRowByKey(ViewData["Receta"]);
                }
            };      
           
        });

    if (ViewData["EditError"] != null){
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}

@grid.Bind(Model).GetHtml()





